package com.pic.mgt.utils;

import java.io.File;

public abstract class FilePathUtils {
	public static String getTopParentName(String path) {
		File topFile = new File(path);
		File tmp = null;
		while((tmp = topFile.getParentFile()) != null) {
			topFile = tmp;
		}
		return topFile.getName();
	}
	
	public static boolean isRoot(String path) {
		boolean check;
		File parent = new File(path).getParentFile();
		if (parent == null) {
			check = true;
		}
		else {
			check = false;
		}
		return check;
	}
	
	public static File multiPathConcat(String ... paths) {
		int len = paths.length;
		File file = new File(paths[0]);		
		for (int i=1;i<len;i++) {
			file = new File(file.getAbsoluteFile(), paths[i]);
		}
		return file;
	}
	
	/**
	 * 檢查目錄，如果目錄不存在則建立目錄結構。
	 * @param path
	 * @return true:目錄不存在並建立成功；false: 目錄已存在，沒有建立目錄
	 * @throws SecurityException
	 */
	public static boolean buildingFoldersIfDoesnotExisted(File path)
												throws SecurityException {
		// 如果目錄不存在，則建立目錄結構
		if (!path.exists()) {
			path.mkdirs();
			return true;
		}
		return false;
	}
}
